//
//  BookItemView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 26.03.25.
//

import SwiftUI

struct BookItemView: View {
    
    static func moreItem (book:AccountingBook, onMoreClick: @escaping ()->Void) -> BookItemView {
        return BookItemView(book: book, viewType: .more, onMoreClick: onMoreClick)
    }
    
    var book: AccountingBook
    var isSelected = false
    var viewType: BookItemViewType = .none
    var onMoreClick:(()->Void)? = nil
    
    var body: some View {
        HStack {
            BookNameView(book: book, style: .large)
            Spacer()
            if isSelected {
                Image("koi_ic_check").resizable().frame(width:22, height: 22)
            }
            if (!book.isDefaultBook()) {
                switch viewType {
                case .more:
                    Button(action: {
                        onMoreClick?()
                    }) {
                        Image("koi_ic_more_horiz").resizable().frame(width:22, height: 22)
                    }
                case .sort:
                    Image("equal.circle.fill").resizable().frame(width:22, height: 22)
                case .none:
                    EmptyView()
                }
            }
        }
    }
}

enum BookItemViewType {
    case none, more, sort
}

@available(iOS 18.0, *)
#Preview {
    @Previewable @State var book = AccountingBook(bookName: "默认账本")
    BookItemView(book: book)
}
